package com.gameley.dmgtpf.root.service.impl;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gameley.dmgtpf.common.utils.PubEnvUtil;
import com.gameley.dmgtpf.root.dao.AdminsDao;
import com.gameley.dmgtpf.root.entity.AdminsEntity;
import com.gameley.dmgtpf.root.service.AdminsService;

@Service("adminsService")
public class AdminsServiceImpl extends ServiceImpl<AdminsDao, AdminsEntity> implements AdminsService
{
    
    @Autowired
    private AdminsDao adminsDao;
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    
    @Override
    public Boolean register(AdminsEntity admin) throws Exception
    {
        lock.lock();
        try
        {
            int count = adminsDao.insert(admin);
            return PubEnvUtil.equals(0, count) ? Boolean.FALSE : Boolean.TRUE;
        }
        catch (Exception e)
        {
            throw new Exception(e);
        }
        finally
        {
            lock.unlock();
        }
        
    }
    
    @Override
    public Boolean userLogin(String uname, String upass) throws NoSuchAlgorithmException
    {
        
        QueryWrapper<AdminsEntity> wrapper = new QueryWrapper();
        wrapper.eq("name", uname).eq("password", uname + PubEnvUtil.md5(upass));
        wrapper.or().eq("password", upass);
        Integer count = adminsDao.selectCount(wrapper);
        
        return PubEnvUtil.equals(0, count) ? Boolean.FALSE : Boolean.TRUE;
    }
    
    @Override
    public List<AdminsEntity> selectObjs()
    {
        List list = adminsDao.selectList(new QueryWrapper());
        
        return list;
    }
    
}
